package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.MembersBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.AddOrDelStudentPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IAddOrDelStudentView;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:下午 3:50.
 * description: ** .
 */
public class AddOrDelStudentFragment extends BaseMvpFragment<IAddOrDelStudentView, AddOrDelStudentPresenter> implements IAddOrDelStudentView, View.OnClickListener {
    private int type;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stv_sub_title)
    SuperTextView stv_sub_title;
    private AODAdapter mAdapter;

    /**
     * @param type 0:从未分组移动到当前组 1，从当前组移出
     */
    public static AddOrDelStudentFragment getInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        AddOrDelStudentFragment fragment = new AddOrDelStudentFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected AddOrDelStudentPresenter createPresenter() {
        return new AddOrDelStudentPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        type = bundle.getInt("type");
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_add_or_del;
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText(type == 0 ? "未进组学生" : "第1组");//这里的数据应从上层传递，或者根据组ID获取
        TextView stv_submit = findViewById(R.id.stv_submit);
        stv_submit.setText(type == 0 ? "确定" : "移出");
        stv_submit.setOnClickListener(this::onClick);
        stv_sub_title.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.stv_submit:
                submit();
                break;
            default:
                if(recyclerView.getVisibility()==View.VISIBLE){
                    recyclerView.setVisibility(View.INVISIBLE);
                    stv_sub_title.setDrawableRotate(-90);
                }else {
                    recyclerView.setVisibility(View.VISIBLE);
                    stv_sub_title.setDrawableRotate(90);
                }
                break;
        }
    }

    private void submit() {
        if (mAdapter == null) {
            return;
        }
        if (mAdapter.set.isEmpty()) {
            ToastUtils.showShort("未选中成员！");
            return;
        }
        if (type == 0) {
            mPresenter.addUserList(mAdapter.set);
        } else {
            mPresenter.removeUserList(mAdapter.set);
        }
    }

    @Override
    public void updataStudent(List<MembersBean> datas) {
        if (mAdapter == null) {
            mAdapter = new AODAdapter(datas);
        }
        recyclerView.setAdapter(mAdapter);
        stv_sub_title.setText(datas.size() + "人");
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init(type);
    }

    @Override
    public void finishAct() {
        getActivity().finish();
    }

    class AODAdapter extends BaseQuickAdapter<MembersBean, BaseViewHolder> {
        Set<MembersBean> set;

        public AODAdapter(@Nullable List<MembersBean> data) {
            super(R.layout.item_cgm_student, data);
            set = new ArraySet<>();
        }

        @Override
        protected void convert(BaseViewHolder helper, MembersBean item) {
            ImageView iv_avatar = helper.getView(R.id.iv_avatar);
            GlideUtils.loadCircleImage(item.getAvatar(), iv_avatar);

            helper.setText(R.id.tv_name, item.getName())
                    .setImageResource(R.id.ic_sel, set.contains(item) ? R.drawable.login_xz : R.drawable.login_wxz);

            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (set.contains(item)) {
                        set.remove(item);
                    } else {
                        set.add(item);
                    }
                    helper.setImageResource(R.id.ic_sel, set.contains(item) ? R.drawable.login_xz : R.drawable.login_wxz);
                }
            });
        }
    }
}
